package cn.itsource.pethome.pet.controller;

import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.PageList;
import cn.itsource.pethome.basic.util.SystemContext;
import cn.itsource.pethome.basic.util.UserContext;
import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.service.IShopService;
import cn.itsource.pethome.pet.domain.Pet;
import cn.itsource.pethome.pet.query.PetQuery;
import cn.itsource.pethome.pet.service.IPetServie;
import cn.itsource.pethome.user.domain.User;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description: 描述:
 * @date: 2020/8/25 11:47
 * @author: summer
 * version: 1.0
 */
@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IShopService shopService;

    @Autowired
    private IPetServie petServie;

    /**
     * 功能描述: 查询所有宠物
     * @param petQuery
     * @return: cn.itsource.pethome.basic.util.PageList<cn.itsource.pethome.pet.domain.Pet>
     * @Date: 2020/8/25
     */
    @PatchMapping("/loadPet")
    public PageList<Pet> loadPet(@RequestBody PetQuery petQuery){
        Shop shop = shopService.loadByEmployee(SystemContext.getSystemLoginUser());
        petQuery.setShopId(shop.getId());
        return petServie.loadAll(petQuery);

    }

    /**
     * 功能描述: 上架
     * @param
     * @return: cn.itsource.pethome.basic.util.AjaxResult
     * @Date: 2020/8/25
     */
    @PatchMapping("/onSale")
    public AjaxResult onSale(@RequestBody List<Pet> pets){
        try {
            petServie.onSale(pets,1);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 功能描述: 下架
     * @param
     * @return: cn.itsource.pethome.basic.util.AjaxResult
     * @Date: 2020/8/25
     */
    @PatchMapping("/offSale")
    public AjaxResult offSale(@RequestBody List<Pet> pets){
        try {
            petServie.offSale(pets,0);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    //===============================================================================

    /**
     * 功能描述: 加载所有宠物
     * @param petQuery
     * @return: cn.itsource.pethome.basic.util.PageList<cn.itsource.pethome.pet.domain.Pet>
     * @Date: 2020/8/25
     */
    @PatchMapping("/loadAll")
    public PageList<Pet> loadAll(@RequestBody PetQuery petQuery){
        petQuery.setState(1);
        return petServie.loadAll(petQuery);
    }



    /**
     * 功能描述: 根据id查询宠物
     * @param id
     * @return: cn.itsource.pethome.pet.domain.Pet
     * @Date: 2020/8/25
     */
    @GetMapping("{id}")
    public Pet loadById(@PathVariable("id") Long id){
        return petServie.loadById(id);
    }

    /**
     * 领养
     * @param id 宠物id
     * @return
     */
    @PostMapping("/adopt/{id}")
    public AjaxResult adopt(@PathVariable("id") Long id,HttpServletRequest request){
        try {
            //拿到当前登录用户
            User loginUser = UserContext.getLoginUser(request);
            petServie.adopt(id,loginUser);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }
}
